package la.renzhen.rtpt.spring.boot.endpoint;

import la.renzhen.rtpt.config.source.SpringConfigurationSource;
import la.renzhen.rtpt.spring.boot.RTPTProperties;
import la.renzhen.rtpt.spring.boot.starter.ConfigurationAutoConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>
 *
 * @author <a href="mailto:zhouhaichao@2008.sina.com">haiker</a>
 * @version 10/05/2018 5:04 PM
 */
@Slf4j
@Configuration
@AutoConfigureAfter(ConfigurationAutoConfiguration.class)
@ConditionalOnClass(name = "org.springframework.boot.actuate.endpoint.Endpoint")
public class EndpointAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(RTPTEndpoint.class)
    public RTPTEndpoint rtptEndpoint(SpringConfigurationSource configurationSource, RTPTProperties config) {
        log.debug("启动RTPT Endpoint");
        String environment = config.getEnvironment();
        String prefix = config.getPrefix();
        return new RTPTEndpoint(configurationSource, environment, prefix);
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnBean(RTPTEndpoint.class)
    @ConditionalOnEnabledEndpoint(RTPTProperties.PREFIX)
    @ConditionalOnWebApplication
    public RTPTMVCEndpoint rtptMvcEndpoint(RTPTEndpoint delegate) {
        return new RTPTMVCEndpoint(delegate);
    }
}
